import utils.FileUtils;
import utils.SystemLookup;

import java.math.BigInteger;
import java.util.Arrays;

public class InsertionSort {
    private static long elapsedNanoTime = 0;
    private static BigInteger elapsedTime = BigInteger.ZERO;

    public static void main(String[] args) {

        if (args.length < 2 || args.length > 12) {
            System.out.println("Nieprawidlowa liczba parametrów!");
        }

        boolean magicImprovement = false;
        if (args[args.length-1].equals("r")) {
            magicImprovement = true;
        }

        boolean mixing = false;
        if (args[args.length-1].equals("m")) {
            mixing = true;
        }

        int numberOfExec = 0;
        try {
            numberOfExec = Integer.parseInt(args[0]);
            System.out.println("Liczba powtórzeń sortowania: " + numberOfExec );
        } catch (Exception ex) {
            System.out.println("Niepopawne parsowanie pierwszego parametru");
            System.exit(1);
        }

        String[] fileNames = Arrays.copyOfRange(args, 1, magicImprovement || mixing ? args.length -1 : args.length);
        System.out.println("Pliki: " + Arrays.toString(fileNames));

        for (String fileName : fileNames) {
            String[] out = FileUtils.readTextFileLine(SystemLookup.PATH_TO_LOAD + fileName + FileUtils.FILE_EXTENSION).split(" ");
            int[] arrayToSort = new int[out.length];
            System.out.println("Plik: " + fileName + "\tElementów: " + out.length);
            for (int i = 0; i < out.length; i++) {
                arrayToSort[i] = Integer.parseInt(out[i]);
            }

            for (int i = 0; i < numberOfExec; i++) {
                int[] intArray = Arrays.copyOf(arrayToSort, arrayToSort.length);
                long timeStart = System.nanoTime();
                if (magicImprovement) {
                    QuickSort.magicImprovement(intArray);
                    System.out.println("Magic attribute");
                }
                if (mixing) {
                    QuickSort.mixing(intArray);
                    System.out.println("Magic attribute");
                }
                insertionSort(intArray);
                elapsedNanoTime = System.nanoTime() - timeStart;
                elapsedTime = elapsedTime.add(BigInteger.valueOf(elapsedNanoTime));
//                System.out.println(Arrays.toString(intArray));
            }
        }

        System.out.println("\nNano Time: " + elapsedTime.toString());
        System.out.println("Milliseconds Time: " + elapsedTime.divide(new BigInteger("1000000")));
        System.out.println("Seconds Time: " + elapsedTime.divide(new BigInteger("1000000000")));
        FileUtils.appendToFile("" + elapsedTime.divide(new BigInteger("1000000")));

    }

    private static void insertionSort(int tablica[]) {
        int i, j, v;

        for (i = 1; i < tablica.length; i++) {
            j = i;
            v = tablica[i];
            while ((j > 0) && (tablica[j - 1] > v)) {
                tablica[j] = tablica[j - 1];
                j--;
            }
            tablica[j] = v;
        }
    }

}
